<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Widget;

use Think\Action;

/**
 * Class SinaFriendWidget
 * @package Help\Widget
 * @author:xjw129xjt xjt@ourstu.com
 */
class SinaFriendWidget extends Action
{

    /* 显示指定分类的同级分类或子分类列表 */
    public function lists($uid)
    {
        $uid = intval($uid) ? intval($uid) : is_login();
        $this->parseContent($uid);
        $this->display('Widget/sinafriend');

    }


    protected function getFriendList($token,$uid,$page){
        $result = D('Addons://SyncLogin/Sina')->getFriendIds($token, $uid, 2000, $page);
        $result_ids = $result['ids'];
        if(!empty($result_ids)){
            $res = $this->getFriendList($token,$uid,$page+1);
            if(!empty($res)){
                $result_ids =array_merge($result_ids,$res);
            }
        }
        return $result_ids;
    }

    public function render($uid)
    {

        $uid = intval($uid) ? intval($uid) : is_login();
        $this->parseContent($uid);
      return   $this->fetch('Widget/sinafriend');

    }

    protected function parseContent($uid){
        $token1 = D('sync_login')->where(array('type' => 'sina', 'uid' => $uid))->find();
        if($token1){
            $token = array('access_token' => $token1['oauth_token'], 'openid' => $token1['oauth_token_secret']);
            $open_ids = $this->getFriendList($token,$token1['oauth_token_secret'],1);
            $sync = D('sync_login')->where(array('type_uid'=>array('in',$open_ids),'type'=>'sina'))->select();
            $uids = getSubByKey($sync,'uid');
            shuffle($uids);
            $uids = array_slice($uids,0,5);
            foreach($uids as $v){
                $user[$v] =  query_user(array('uid', 'username', 'nickname', 'space_url', 'space_link', 'avatar64', 'avatar128', 'rank_html', 'signature','credibility','address','doing','fans','is_following'), $v);
            }

            $this->assign('users',$user);
        }

    }
}
